import type { App } from 'vue'

import type { LocaleSetupOptions, SupportedLanguagesType } from './setup'

import { $t, setupI18n as coreSetup, loadLocalesMapFromDir } from './setup'

const modules = import.meta.glob('./langs/**/*.json')

const localesMap = loadLocalesMapFromDir(
  /\.\/langs\/([^/]+)\/(.*)\.json$/,
  modules
)

/**
 * 加载应用特有的语言包
 * 这里也可以改造为从服务端获取翻译数据
 * @param lang
 */
async function loadMessages(lang: SupportedLanguagesType) {
  const appLocaleMessages = await localesMap[lang]?.()
  return appLocaleMessages?.default
}

async function setupI18n(app: App, options: LocaleSetupOptions = {}) {
  await coreSetup(app, {
    defaultLocale: 'zh-CN',
    loadMessages,
    missingWarn: !import.meta.env.PROD,
    ...options,
  })
}

export { $t, setupI18n }
